package leetcode.dichotomy.democ;

import java.util.HashMap;

/**
 * @author cyt
 * @create 2020-09-21 17:24
 */
public class Ta {

    public static void main(String[] args) {



        System.out.println(Math.round(11.5));

        HashMap<Object, Object> map = new HashMap<>();
        int $dd = 1;
        StringBuffer buffer = new StringBuffer(10);
        buffer.append("1234");

        System.out.println(buffer.length());
        System.out.println(buffer.capacity());

        int[] ints = {1, 2, 3, 4, 5, 6};
        Ta ta = new Ta();
        int binarySearch = ta.binarySearch(ints, 4);
        System.out.println(binarySearch);
    }


    int binarySearch(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }

        int left = 0, right = nums.length - 1;
        while (left + 1 < right) {
            // Prevent (left + right) overflow
            int mid = left + (right - left) / 2;
            System.out.println("left- > " + left + " mid - > " + mid + " right - > " + right);
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid;
            } else {
                right = mid;
            }
        }

        // Post-processing:
        // End Condition: left + 1 == right
        if (nums[left] == target) {
            return left;
        }
        if (nums[right] == target) {
            return right;
        }
        return -1;
    }
}
